#LOCALE?=en_IN
LOCALE?=$(shell locale | sed -n '1s/LANG=\([^.]*\).*/\1/p')

ROOT_DIR:=../../..
ENGINE_DIR:=$(ROOT_DIR)/naimath/engine
TOOL_DIR:=$(ROOT_DIR)/tools
KRB_DIRS:=$(ENGINE_DIR)

PO_DIR:=$(LOCALE)
MO_DIR:=../$(LOCALE)/LC_MESSAGES

KRB_FILES=$(wildcard $(KRB_DIRS)/*.krb)
EXTRACTED_QUESTIONS=$(ENGINE_DIR)/extracted_questions.py
POT_FILES=$(wildcard *.pot)
GENERATED_POT_FILES=$(filter-out diagnosis.pot standard.pot,$(POT_FILES))
PO_FILES=$(POT_FILES:%.pot=$(PO_DIR)/%.po)
MO_FILES=$(PO_FILES:$(PO_DIR)/%.po=$(MO_DIR)/%.mo)

EXTRACT_QUESTIONS:=$(TOOL_DIR)/extract_questions.py
MAKEPOT:=$(TOOL_DIR)/makepot.py

.PHONY: extract pot po mo test

extract: $(EXTRACTED_QUESTIONS) diagnosis.pot

pot: $(POT_FILES)

po: $(PO_FILES)

mo: $(MO_FILES)

$(EXTRACTED_QUESTIONS) diagnosis.pot: $(KRB_FILES)
	$(EXTRACT_QUESTIONS) $(KRB_FILES)

$(GENERATED_POT_FILES): $(EXTRACTED_QUESTIONS)
	$(MAKEPOT)

$(PO_DIR)/%.po: %.pot
	if [ -e $@ ]; then                                \
	    msgmerge --update --no-fuzzy-matching $@ $<;  \
	else                                              \
	    msginit -l $(LOCALE) -i $< -o $@;             \
	fi

$(MO_DIR)/%.mo: $(PO_DIR)/%.po
	msgfmt -o $@ $<

test:
	@echo LOCALE $(LOCALE)
	@echo KRB_FILES $(KRB_FILES)
	@echo POT_FILES $(POT_FILES)
	@echo GENERATED_POT_FILES $(GENERATED_POT_FILES)
	@echo PO_FILES $(PO_FILES)
	@echo MO_FILES $(MO_FILES)
